<?php
	session_start();
	if(!array_key_exists("role", $_SESSION) || $_SESSION['role'] != "admin"){
		header("Location: /index.php");
		exit;
	}
	
	$conn = mysql_connect("", "piedu_user", "q&&^%@8ezO8+");
	mysql_select_db("piedu_db");
	
	if(array_key_exists("action", $_GET) &&  array_key_exists("id", $_GET) && $_GET['action'] == "delete"){
		$sql = "DELETE FROM coupons WHERE id = {$_GET['id']}";
		mysql_query($sql, $conn);
		header("Location: coupons.php?deleted=true");
		exit;
	} else if(array_key_exists("action", $_GET) &&  array_key_exists("id", $_GET) && $_GET['action'] == "edit"){
		$id = preg_replace("/[^0-9\s]/", "", $_GET['id']);
		$sql = "SELECT * from coupons WHERE id = $id";
		$query = mysql_query($sql, $conn);
		if(mysql_num_rows($query) < 1){
			header("Location: coupons.php?notfound=true");
			exit;
		} else {
			$coupon = mysql_fetch_row($query);
		}
	} else if(array_key_exists("action", $_POST) && $_POST['action'] == "add"){
		$start_date = date("Y-m-d", mktime(0,0,0, $_POST['start_month'], $_POST['start_day'], $_POST['start_year']));
		$end_date = date("Y-m-d", mktime(0,0,0, $_POST['end_month'], $_POST['end_day'], $_POST['end_year']));
		$sql = "INSERT INTO coupons (code, start_date, end_date, coupon_type, minimum_purchase, discount_type, discount_amount, apply_to, items) VALUES ('{$_POST['code']}', TIMESTAMP('$start_date'), TIMESTAMP('$end_date'), '{$_POST['coupon_type']}', {$_POST['minimum_purchase']}, '{$_POST['discount_type']}', {$_POST['discount_amount']}, '{$_POST['apply_to']}', '{$_POST['items']}')";
		mysql_query($sql, $conn);
		header("Location: coupons.php?added=true");
		exit;
	} else if(array_key_exists("action", $_POST) && $_POST['action'] == "update"){
		$start_date = date("Y-m-d", mktime(0,0,0, $_POST['start_month'], $_POST['start_day'], $_POST['start_year']));
		$end_date = date("Y-m-d", mktime(0,0,0, $_POST['end_month'], $_POST['end_day'], $_POST['end_year']));
		$sql = "UPDATE coupons SET code = '{$_POST['code']}', start_date ='$start_date', end_date = '$end_date', coupon_type = '{$_POST['coupon_type']}', minimum_purchase = {$_POST['minimum_purchase']}, discount_type = '{$_POST['discount_type']}', discount_amount = {$_POST['discount_amount']}, apply_to = '{$_POST['apply_to']}', items = '{$_POST['items']}' WHERE id = '{$_POST['id']}'";
		mysql_query($sql, $conn);
		header("Location: coupons.php?added=true");
		exit;
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pie Education | Coupons</title>
<Meta name="Keywords" content="education, course, learning, online, virtual, e-learning, school, student, teacher, homework, educational platform, educational system, free tool">
<Meta name="Description" content="Pie Education is a internet based Course Management System (CMS)">
<Meta name="Author" content="Rob Keller">
<Meta name="Publisher" content="Aaron Aceves">
<Meta name="Copyright" content="Copyright© 2012, Pie Education">
<Meta name="Revisit-After" content="30 days">
<Meta HTTP-EQUIV="Expires" content="none">
<Meta name="Pagetopic" content="Education">
<Meta name="Audience" content=" All">
<Meta name="Robots" content="INDEX,FOLLOW">
<Meta name="Content-Language" content="English">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" href="../css/style.css" />
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="80" bgcolor="#FF8C00" align="center"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><a href="index.php"><img src="../images/logo.png" alt="Return to Index" width="250" height="70" border="0" title="pie.edu an E-Learning Network" /></a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="30" bgcolor="#000000" align="center"><a href="index.php" class="global-nav-menu" style="color:#FFF">Home </a><a href="coupons.php" class="global-nav-menu" style="color:#FFF">Manage Coupons </a><a href="promotions.php" class="global-nav-menu" style="color:#FFF">Manage Promotions </a><a href="items.php" class="global-nav-menu" style="color:#FFF">Manage Items</a><a href="manage_orders.php" class="global-nav-menu" style="color:#FFF">Manage Orders</a><a href="transactions.php" class="global-nav-menu" style="color:#FFF">Transactions Details</a><a href="#" class="global-nav-menu" style="color:#FFF">Another Menu</a><a href="#" class="global-nav-menu" style="color:#FFF">Another Menu</a><a href="#" class="global-nav-menu" style="color:#FFF">Another Menu</a><a href="/logout.php" class="global-nav-menu" style="color:#FFF">Logout</a></td>
  </tr>
  <tr>
    <td height="500" bgcolor="#FFFFFF"><table width="980" border="0" align="center" cellpadding="4" cellspacing="4">
      <tr>
        <td>&nbsp;</td>
      </tr>
      <?php
	  if($_GET['action'] == "edit"){
		  $start_date_explode = explode(" ", $coupon[2]);
		  list($start_year, $start_month, $start_day) = explode("-", $start_date_explode[0]);
		  $end_date_explode = explode(" ", $coupon[3]);
		  list($end_year, $end_month, $end_day) = explode("-", $end_date_explode[0]);
		  $month_array = array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Agu", "Sep", "Oct", "Nov", "Dec");
		  $counter = 1;
      echo '<tr>
        <td><form id="form2" name="form2" method="post" action="coupons_action.php">
          <table width="600" border="0" align="center" cellpadding="4" cellspacing="2">
            <tr>
              <td width="150"><strong>Coupon Name</strong></td>
              <td><label for="code"></label>
                <input name="code" type="text" id="code" size="40" value="'.$coupon[1].'" />
                <input name="action" type="hidden" id="action" value="update" />
				<input name="id" type="hidden" id="id" value="'.$coupon[0].'" /></td>
            </tr>
            <tr>
              <td><strong>Start Date</strong></td>
              <td><table width="250" border="0" align="left" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="33%"><select name="start_day" id="start_day">';
				  for($i = 1; $i < 32; $i++){
					echo '<option value="'.$i.'"'; if($i == $start_day){ echo 'selected="selected"'; } echo '>'.$i.'</option>';
				  }
                  echo '</select></td>
                  <td width="33%"><select name="start_month" id="start_month">';
                  foreach($month_array as $value){
					  echo '<option value="'.$counter.'"'; if($counter == $start_month) { echo 'selected="selected"'; } echo'>'.$value.'</option>';
					  $counter++;
				  }
                  echo '</select></td>
                  <td width="34%"><select name="start_year" id="start_year">';
                    for($i = 2012; $i < 2020; $i++){
						echo '<option value="'.$i.'"'; if($i == $start_year){ echo 'selected="selected"'; } echo '>'.$i.'</option>';
					}
                  echo '</select></td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td><strong>End Date</strong></td>
              <td><table width="250" border="0" align="left" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="33%"><select name="end_day" id="end_day">';
				  for($i = 1; $i < 32; $i++){
					echo '<option value="'.$i.'"'; if($i == $end_day){ echo 'selected="selected"'; } echo '>'.$i.'</option>';
				  }
                  echo '</select></td>
                  <td width="33%"><select name="end_month" id="end_month">';
                  $counter = 1;
				  foreach($month_array as $value){
					  echo '<option value="'.$counter.'"'; if($counter == $end_month) { echo 'selected="selected"'; } echo'>'.$value.'</option>';
					  $counter++;
				  }
                  echo '</select></td>
                  <td width="34%"><select name="end_year" id="end_year">';
                    for($i = 2012; $i < 2020; $i++){
						echo '<option value="'.$i.'"'; if($i == $end_year){ echo 'selected="selected"'; } echo '>'.$i.'</option>';
					}
                  echo '</select></td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td><strong>Coupon Type</strong></td>
              <td><select name="coupon_type" id="coupon_type">
                <option value="peritem"'; if($coupon[4] == "peritem"){ echo 'selected="selected"'; } echo '>Per Item Discount</option>
                <option value="general"'; if($coupon[4] == "general"){ echo 'selected="selected"'; } echo '>Total Order Discount</option>
              </select></td>
            </tr>
            <tr>
              <td><strong>Minimum Purchase</strong></td>
              <td><label for="minimum_purchase"></label>
                <input name="minimum_purchase" type="text" id="minimum_purchase" value="'.$coupon[5].'" size="10" /></td>
            </tr>
            <tr>
              <td><strong>Discount Type</strong></td>
              <td><label for="discount_type"></label>
                <select name="discount_type" id="discount_type">
                  <option value="USD"'; if($coupon[6] == "USD"){ echo 'selected="selected"'; } echo '>Dollar Amount</option>
                  <option value="percentage"'; if($coupon[6] == "percentage"){ echo 'selected="selected"'; } echo '>Percentage</option>
                </select></td>
            </tr>
            <tr>
              <td><strong>Discount Amount</strong></td>
              <td><label for="discount_amount"></label>
                <input name="discount_amount" type="text" id="discount_amount" value="'.$coupon[7].'" size="10" /></td>
            </tr>
            <tr>
              <td><strong>Apply To</strong></td>
              <td><label for="apply_to"></label>
                <select name="apply_to" id="apply_to">
                  <option value="everyone"'; if($coupon[8] == "everyone"){ echo 'selected="selected"'; } echo '>Everyone</option>
                  <option value="student"'; if($coupon[8] == "student"){ echo 'selected="selected"'; } echo '>Students</option>
                  <option value="teacher"'; if($coupon[8] == "teacher"){ echo 'selected="selected"'; } echo '>Teachers</option>
                  <option value="developer"'; if($coupon[8] == "developer"){ echo 'selected="selected"'; } echo '>Developers</option>
                  <option value="admin"'; if($coupon[8] == "admin"){ echo 'selected="selected"'; } echo '>Administrators</option>
                </select></td>
            </tr>
            <tr>
              <td><strong>Items</strong></td>
              <td><label for="items"></label>
                <textarea name="items" id="items" cols="45" rows="3">'.$coupon[9].'</textarea></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><input name="submit2" type="submit" class="submit" id="submit2" value="Update Coupon" /></td>
            </tr>
          </table>
        </form></td>
      </tr>';
      } else {
      echo '<tr>
        <td><form id="form1" name="form1" method="post" action="coupons_action.php">
          <table width="600" border="0" align="center" cellpadding="4" cellspacing="2">
            <tr>
              <td width="150"><strong>Coupon Name</strong></td>
              <td><label for="code"></label>
                <input name="code" type="text" id="code" size="40" />
                <input name="action" type="hidden" id="action" value="add" /></td>
            </tr>
            <tr>
              <td><strong>Start Date</strong></td>
              <td><table width="250" border="0" align="left" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="33%"><select name="start_day" id="start_day">
<option value="1" selected="selected">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    <option value="13">13</option>
                    <option value="14">14</option>
                    <option value="15">15</option>
                    <option value="16">16</option>
                    <option value="17">17</option>
                    <option value="18">18</option>
                    <option value="19">19</option>
                    <option value="20">20</option>
                    <option value="21">21</option>
                    <option value="22">22</option>
                    <option value="23">23</option>
                    <option value="24">24</option>
                    <option value="25">25</option>
                    <option value="26">26</option>
                    <option value="27">27</option>
                    <option value="28">28</option>
                    <option value="29">29</option>
                    <option value="30">30</option>
                    <option value="31">31</option>
                  </select></td>
                  <td width="33%"><select name="start_month" id="start_month">
<option value="1" selected="selected">Jan</option>
                    <option value="2">Feb</option>
                    <option value="3">Mar</option>
                    <option value="4">Apr</option>
                    <option value="5">May</option>
                    <option value="6">Jun</option>
                    <option value="7">Jul</option>
                    <option value="8">Agu</option>
                    <option value="9">Sep</option>
                    <option value="10">Oct</option>
                    <option value="11">Nov</option>
                    <option value="12">Dec</option>
                  </select></td>
                  <td width="34%"><select name="start_year" id="start_year">
                    <option value="2012" selected="selected">2012</option>
                    <option value="2013">2013</option>
                    <option value="2014">2014</option>
                    <option value="2015">2015</option>
                  </select></td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td><strong>End Date</strong></td>
              <td><table width="250" border="0" align="left" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="33%"><select name="end_day" id="end_day">
                    <option value="1" selected="selected">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    <option value="13">13</option>
                    <option value="14">14</option>
                    <option value="15">15</option>
                    <option value="16">16</option>
                    <option value="17">17</option>
                    <option value="18">18</option>
                    <option value="19">19</option>
                    <option value="20">20</option>
                    <option value="21">21</option>
                    <option value="22">22</option>
                    <option value="23">23</option>
                    <option value="24">24</option>
                    <option value="25">25</option>
                    <option value="26">26</option>
                    <option value="27">27</option>
                    <option value="28">28</option>
                    <option value="29">29</option>
                    <option value="30">30</option>
                    <option value="31">31</option>
                  </select></td>
                  <td width="33%"><select name="end_month" id="end_month">
                    <option value="1" selected="selected">Jan</option>
                    <option value="2">Feb</option>
                    <option value="3">Mar</option>
                    <option value="4">Apr</option>
                    <option value="5">May</option>
                    <option value="6">Jun</option>
                    <option value="7">Jul</option>
                    <option value="8">Agu</option>
                    <option value="9">Sep</option>
                    <option value="10">Oct</option>
                    <option value="11">Nov</option>
                    <option value="12">Dec</option>
                  </select></td>
                  <td width="34%"><select name="end_year" id="end_year">
                    <option value="2012" selected="selected">2012</option>
                    <option value="2013">2013</option>
                    <option value="2014">2014</option>
                    <option value="2015">2015</option>
                  </select></td>
                </tr>
              </table></td>
            </tr>
            <tr>
              <td><strong>Coupon Type</strong></td>
              <td><select name="coupon_type" id="coupon_type">
                <option value="peritem" selected="selected">Per Item Discount</option>
                <option value="general">Total Order Discount</option>
              </select></td>
            </tr>
            <tr>
              <td><strong>Minimum Purchase</strong></td>
              <td><label for="minimum_purchase"></label>
                <input name="minimum_purchase" type="text" id="minimum_purchase" value="0.0" size="10" /></td>
            </tr>
            <tr>
              <td><strong>Discount Type</strong></td>
              <td><label for="discount_type"></label>
                <select name="discount_type" id="discount_type">
                  <option value="USD" selected="selected">Dollar Amount</option>
                  <option value="percentage">Percentage</option>
                </select></td>
            </tr>
            <tr>
              <td><strong>Discount Amount</strong></td>
              <td><label for="discount_amount"></label>
                <input name="discount_amount" type="text" id="discount_amount" value="0.0" size="10" /></td>
            </tr>
            <tr>
              <td><strong>Apply To</strong></td>
              <td><label for="apply_to"></label>
                <select name="apply_to" id="apply_to">
                  <option value="everyone" selected="selected">Everyone</option>
                  <option value="student">Students</option>
                  <option value="teacher">Teachers</option>
                  <option value="developer">Developers</option>
                  <option value="admin">Administrators</option>
                </select></td>
            </tr>
            <tr>
              <td><strong>Items</strong></td>
              <td><label for="items"></label>
                <textarea name="items" id="items" cols="45" rows="3"></textarea></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><input name="submit" type="submit" class="submit" id="submit" value="Create Coupon" />
                <input name="Reset" type="reset" class="submit" id="Reset" value="Reset Form" /></td>
            </tr>
          </table>
        </form></td>
      </tr>';
      }
      ?>
      <tr>
        <td>&nbsp;</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="80" bgcolor="#FF9933" align="center"><a href="../index.php" class="bottom-general-menu" style="color:#FFF">Home</a> <a href="../platform.php" class="bottom-general-menu" style="color:#FFF">Platform</a> <a href="../piephone.php" class="bottom-general-menu" style="color:#FFF">Pie Phone</a> <a href="../company_training.php" class="bottom-general-menu" style="color:#FFF">Company Training</a> <a href="../jobs.php" class="bottom-general-menu" style="color:#FFF">Jobs</a> <a href="../press.php" class="bottom-general-menu" style="color:#FFF">Press</a> <a href="../community/" class="bottom-general-menu" style="color:#FFF">Community</a> <a href="../mobile.php" class="bottom-general-menu" style="color:#FFF">Mobile</a> <a href="support/" class="bottom-general-menu" style="color:#FFF">Help</a> <a href="../blog/" class="bottom-general-menu" style="color:#FFF">Blogs</a> <a href="../forums/" class="bottom-general-menu" style="color:#FFF">Forums</a><a href="../about.php" class="bottom-general-menu" style="color:#FFF">About</a> <a href="../terms.html" class="bottom-general-menu" style="color:#FFF">Terms of Use</a> <a href="../privacy.html" class="bottom-general-menu" style="color:#FFF">Privacy Policy</a> <a href="../contact-us.php" class="bottom-general-menu" style="color:#FFF">Contact Us</a></td>
  </tr>
</table>
</body>
</html>